package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Orders;
import com.luchao.back.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "订单管理")
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("分页获取订单信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5"),
            @ApiImplicitParam(name = "ouid", value = "用户Id", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = ""),
            @ApiImplicitParam(name = "ostate", value = "订单状态", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getOrdersByPage")
    public ResponseResult getTypesByPage(Integer pageNum, Integer pageSize, Integer ouid, Integer ostate) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = ordersService.getOrdersCount(ouid, ostate);
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Orders> pageResult = new PageResult<Orders>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(ordersService.getOrdersByPage(pageNum, pageSize, ouid, ostate));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("变更订单状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "oid", value = "订单Id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = ""),
            @ApiImplicitParam(name = "ouid", value = "订单用户Id(ostate为1、4时必须填写)", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = ""),
            @ApiImplicitParam(name = "ointegral", value = "订单积分(ostate为1、4时必须填写)", required = false, paramType = "query", dataType = "Float", dataTypeClass = Float.class, defaultValue = ""),
            @ApiImplicitParam(name = "ostate", value = "变更订单目标状态", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/changeOstate")
    public ResponseResult changeOstate(Integer oid, Integer ouid, Float ointegral, Integer ostate) {
        return ordersService.changeOstate(oid, ouid, ointegral, ostate) ? ResponseResult.success(ResponseResult.SUCCESS, "订单状态已更新", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "订单状态更新失败", Execute.ERROR);
    }

    @ApiOperation("创建订单")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orders", value = "订单信息", required = true, paramType = "formData", dataType = "Orders", dataTypeClass = Orders.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/createOrders")
    public ResponseResult createOrders(@RequestBody Orders orders) {
        Orders data = ordersService.addOrders(orders);
        return data != null ? ResponseResult.success(ResponseResult.SUCCESS, "订单已创建", data) : ResponseResult.success(ResponseResult.FAILED, "订单创建失败", null);
    }

    @ApiOperation("根据订单Id获取订单详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "oid", value = "订单信息", required = true, paramType = "query", dataType = "int", dataTypeClass = int.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getOrdersDetaileById")
    public ResponseResult getOrdersDetaileById(int oid) {
        return ResponseResult.success(ordersService.getOrdersDetaileById(oid));
    }
}
